Mo's algorithm